/*
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

package ru.ssau.berlin.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Caesar
 */
@Entity
@NamedQueries(
    {@NamedQuery(
        name = "findGroupTypes",
        query = "SELECT object(gt) FROM GroupType gt"
    ),
    @NamedQuery(
       name = "findGroupTypeByName",
       query = "SELECT object(gt) FROM GroupType gt WHERE gt.name LIKE :typeName"
    )}
)
public class GroupType implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private GroupType parent;

    @OneToMany(mappedBy="parent", cascade=CascadeType.REMOVE)
    private List<GroupType> children;

    @OneToMany(mappedBy = "type")
    private List<PersonGroup> groups;

    @ManyToMany(fetch=FetchType.EAGER)
    private List<GroupAttribute> attributes;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<GroupType> getChildren() {
        return children;
    }

    public void setChildren(List<GroupType> children) {
        this.children = children;
    }

    public GroupType getParent() {
        return parent;
    }

    public void setParent(GroupType parent) {
        this.parent = parent;
    }

    public List<PersonGroup> getGroups() {
        return groups;
    }

    public List<GroupAttribute> getAttributes() {
        return attributes;
    }

    @PostLoad
    public void postLoad() {
        children.size();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GroupType)) {
            return false;
        }
        GroupType other = (GroupType) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return this.name + "[" + this.id + "]";
    }

}
